#include "findscore.h"
#include "ui_findscore.h"
#include <QMessageBox>
#include "mainwindow.h"
#include "globle.h"
#include <QtDebug>
#include "studentform.h"
findscore::findscore(int index,QSqlDatabase* db,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::findscore)
{
    ui->setupUi(this);


    m_db = QSqlDatabase::addDatabase("QMYSQL");
    m_db.setHostName("localhost");
    m_db.setDatabaseName("studentmanager");
    m_db.setUserName(sqluser);
    m_db.setPassword(sqlpass);
    if (!m_db.open())
        qDebug() << "Failed to connect to root mysql admin";
    else qDebug() << "open";



    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    m_index=index;
    if(index==0){//学生端
        QStandardItemModel* model=new QStandardItemModel(this);
        model->setColumnCount(6);
        model->setHeaderData(0,Qt::Horizontal,"ID");
        model->setHeaderData(1,Qt::Horizontal,"name");
        model->setHeaderData(2,Qt::Horizontal,"banji");
        model->setHeaderData(3,Qt::Horizontal,"time");
        model->setHeaderData(4,Qt::Horizontal,"object");
        model->setHeaderData(5,Qt::Horizontal,"score");
        QSqlQuery query(m_db);
        query.exec("select * from student where name='"+username_qj+"';");
        query.first();
        QString classId=query.value("banji").toString();
        query.clear();
        query.exec("select * from score where banji="+classId+";");

        for(;query.next();){

            model->insertRow(model->rowCount());
            int row=model->rowCount()-1;
            model->setData(model->index(row,0),query.value("id"));
            model->setData(model->index(row,1),username_qj);
            model->setData(model->index(row,2),query.value("banji"));
            model->setData(model->index(row,3),query.value("time"));
            model->setData(model->index(row,4),query.value("object"));
            model->setData(model->index(row,5),query.value("score"));
        }


        ui->tableView->setModel(model);


        ui->pushButton->hide();
        ui->pushButton_2->hide();
        ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    }else if(index==1){//教师端
        QSqlTableModel* model=new QSqlTableModel(this,m_db);
        model->setTable("score");
        model->select();
        ui->tableView->setModel(model);
    }


        // m_db.close();
}

findscore::~findscore()
{
    delete ui;
}

// void findscore::on_findscoreButton_clicked()
// {
//     QString id=ui->lineEdit_findscore_id->text();
//     QString object=ui->lineEdit_findscore_object->text();
//     QString time=ui->lineEdit_findscore_time->text();
//     if(id.compare("")==0||object.compare("")==0||time.compare("")==0)
//     QMessageBox::information(this, "警告", "查询条件输入不足");

//     QSqlDatabase db;
//     if(QSqlDatabase::contains("qt_sql_default_connection"))
//     db = QSqlDatabase::database("qt_sql_default_connection");
//     else
//     db = QSqlDatabase::addDatabase("QMYSQL");
//     db.setHostName("localhost");
//     db.setDatabaseName("studentmanager");
//     db.setUserName(sqluser);
//     db.setPassword(sqlpass);
//     if (!db.open())
//     qDebug() << "Failed to connect to root mysql admin";
//      else qDebug() << "open";


//     QSqlQuery query(db);
//     db.exec("SET NAMES 'GBK'");
//   qDebug()<<":"<<id<<":"<<object<<":"<<time;

//    query.exec("select id,object,time,banji,teacher,score from score where id='"+id+"' and object='"+object+"' and time='"+time+"'");



//     if(query.next()){
//        qDebug()<<"第一条数据为"<<query.value(0).toString();
//         ui->lineEdit_findscore_id->setText(query.value(0).toString());
//         ui->lineEdit_findscore_object->setText(query.value(1).toString());
//         ui->lineEdit_findscore_time->setText(query.value(2).toString());
//         ui->lineEdit_findscore_class->setText(query.value(3).toString());
//         ui->lineEdit_findscore_teacher->setText(query.value(4).toString());
//         ui->lineEdit_findscore_score->setText(query.value(5).toString());



//     }
//         else
//          {
//            if(id.compare("")==0||object.compare("")==0||time.compare("")==0)

//                      return;

//            else   QMessageBox::information(this, "警告", "不存在你所要查找的数据");

//          }
// }

void findscore::on_pushButton_findcore_back_clicked()
{
    this->hide();
    if(m_index==0){//学生端
        studentForm *ww=new studentForm;
        ww->show();
    }else{//教师端
        teacherForm* t=new teacherForm;
        t->show();
    }

}

void findscore::on_pushButton_clicked()
{
    if(ui->tableView->currentIndex().isValid()){
        ui->tableView->model()->removeRow(ui->tableView->currentIndex().row());
        ui->tableView->setCurrentIndex(QModelIndex());
    }
}


void findscore::on_pushButton_2_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tableView->model());
    if(!model->isDirty()){
        QMessageBox::warning(this,"错误","数据未做修改.");
        return;
    }
    int res=QMessageBox::question(this,"询问","你确定要提交修改吗?");
    if(res==QMessageBox::Yes){
        model->submitAll();
    }
}

